package ee.battleship.classes;

import java.util.ArrayList;
import java.util.Comparator;

public class Player {
    private int id;
    private String name;
    private int score;
    private int wins;
    private int playedGames;
    private int losses;
    private ArrayList<Fleet> fleets = new ArrayList<Fleet>();
    private ArrayList<Fleet> mySunkenFleets = new ArrayList<Fleet>();
    private ArrayList<Shot> enemyShots = new ArrayList<Shot>(); 
    private boolean newEnemyShots = false;
    
    public Player(int _id) {
    	this.score = 0;
        this.id = _id;
    }

    public int getScore() {
		return score;
	}

	public void setScore(int score) {
		this.score = score;
	}

	public void increaseScore() {
		setScore(getScore() + 1);
    }
    
    public int getId() {
        return id;
    }
    
    public ArrayList<Fleet> getFleets() {
        return fleets;
    }
    
    public void addShip(int id, int size, int top, int left, int orientation) {
        fleets.add(new Fleet(id, size, top, left, orientation));
        return;
    }
    
    public void shipSunken(Fleet fleet) {
        mySunkenFleets.add(fleet);
        return;
    }
    
    public Fleet getSunkenShip() {
        return mySunkenFleets.get(mySunkenFleets.size()-1);
    }
    
    public boolean newEnemyShots() {
        return newEnemyShots;
    }
    
    public Shot getEnemyShot() {
        if ((enemyShots.size()-1) == 0) {
                newEnemyShots = false;
        }
        int x = enemyShots.get(enemyShots.size()-1).getX();
        int y = enemyShots.get(enemyShots.size()-1).getY();
        int status = enemyShots.get(enemyShots.size()-1).getStatus();
        int id = enemyShots.get(enemyShots.size()-1).getID();
        Shot enemyShot = new Shot(x, y, status, id);
        enemyShots.remove(enemyShots.size()-1);
        return enemyShot;
    }
    
    public int checkHit(int left, int top) { // 0 - miss, 1 - hit, 2 - sank
        newEnemyShots = true;
        for(int i = 0; i < 10; i++) {
            int x1 = -1;
            int x2 = -1;
            int y1 = -1;
            int y2 = -1;
            if(fleets.get(i).getOrientation() == 1) {
                x1 = fleets.get(i).getLeft();
                x2 = fleets.get(i).getLeft() + fleets.get(i).getSize();
                y1 = fleets.get(i).getTop();
                y2 = fleets.get(i).getTop() + 1;
            }
            else if(fleets.get(i).getOrientation() == 0) {
                x1 = fleets.get(i).getLeft();
                x2 = fleets.get(i).getLeft() + 1;
                y1 = fleets.get(i).getTop();
                y2 = fleets.get(i).getTop() + fleets.get(i).getSize();
            }
            if((x1 < (left + 1)) && (x2 > left) && (y1 < (top + 1)) && (y2 > top)) {
                int fleetStatus = fleets.get(i).hitConfirmed();
                if (fleetStatus == 2) {
                    shipSunken(fleets.get(i));
                }
                enemyShots.add(new Shot(left, top, fleetStatus, fleets.get(i).getId()));
                return fleetStatus;
            }
        }
        enemyShots.add(new Shot(left, top, 0, -1));
        return 0;
    }

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getWins() {
		return wins;
	}

	public void setWins(int wins) {
		this.wins = wins;
	}

	public int getLosses() {
		return losses;
	}

	public void setLosses(int losses) {
		this.losses = losses;
	}

	public void increaseLosses() {
		setLosses(getLosses() + 1); 
	}
	
	public void increaseWins() {
		setWins(getWins() + 1);
	}
	
	public void increasePlayedGames() {
		setPlayedGames(getPlayedGames() + 1);
	}
	
	public int getPlayedGames() {
		return playedGames;
	}

	public void setPlayedGames(int playedGames) {
		this.playedGames = playedGames;
	}
	
	public void resetPlayerMoves() {
		this.fleets = new ArrayList<Fleet>();
		this.mySunkenFleets = new ArrayList<Fleet>();
		this.enemyShots = new ArrayList<Shot>();
	}

}
